
$(function () {
    $.ajax({
        type: "get",
        url: "/sms/admin/studentLecture/listStudentLecture",
        dataType: "json",
        success: function (wechatReturn) {
            var eMap = wechatReturn.data;
            initLecture(eMap);
        },
        error: function (wechatReturn) {
            $.alert("获取讲座失败！");
        }
    });

});

function initLecture(eMap) {
    var tableLectureLatest = document.getElementById("tab-lecture-latest");
    var tableLectureUnattend = document.getElementById("tab-lecture-unattend");
    var tableLectureAttended = document.getElementById("tab-lecture-attended");
    var latestLecture = eMap["latestLecture"];

    var underwayLectureDto = eMap["underwayLectureDto"];
    var expectedLectureIds = eMap["expectedLectureIds"];
    var attendedLecture = eMap["attendedLecture"];

    for(var i=0; i<latestLecture.length; i++){
        var e = latestLecture[i];

        // 根div
        var div_root = document.createElement("div");
        div_root.setAttribute("class", "weui-panel");
        //     链接（海报和标题）
        var a_poster = document.createElement("a");
        a_poster.setAttribute("href", e.wxNewsUrl);
        //         链接--海报
        var div_image = document.createElement("div");
        div_image.setAttribute("class", "poster_image");
        var image = document.createElement("img");
        image.setAttribute("src", "/sms/resources/wechat/upload/images/" + e.snapshoot);
        div_image.appendChild(image);
        //         链接--标题
        var p_title = document.createElement("p");
        p_title.setAttribute("class", "poster_title");
        p_title.innerHTML = e.title;

        a_poster.appendChild(div_image);
        a_poster.appendChild(p_title);
        div_root.appendChild(a_poster);

        //     讲座信息和操作
        var div_lecture = document.createElement("div");
        div_lecture.setAttribute("class", "poster_text");
        //         讲座信息
        var span_lecture = document.createElement("span");
        span_lecture.setAttribute("class", "weui-media-box__desc");
        var st = new Date(e.startTime);
        var minutes = st.getMinutes();
        if(minutes==0){
            minutes="00";
        }
        var startTime = st.getFullYear()+"-"+(st.getMonth()+1)+"-"+st.getDate()+" "+st.getHours()+":"+minutes;
        span_lecture.innerHTML = "主讲人:" + e.speaker + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;时间:" + startTime;
        //         讲座操作
        var a_lecture = document.createElement("a");
        a_lecture.setAttribute("href", "javascript:void(0);");
        a_lecture.setAttribute("class", "weui-btn weui-btn_mini weui-btn_primary");
        a_lecture.setAttribute("id", "lecture" + e.id);
        if($.inArray(e.id, expectedLectureIds)==-1){
            a_lecture.innerHTML = "参加";
            a_lecture.setAttribute("onclick", "takeLecture(" + e.id + " )");
        }else{
            a_lecture.innerHTML = "已报名";
            a_lecture.setAttribute("class", "weui-btn weui-btn_mini weui-btn_primary weui-btn_disabled");
        }
        div_lecture.appendChild(span_lecture);
        div_lecture.appendChild(a_lecture);

        div_root.appendChild(a_poster);
        div_root.appendChild(div_lecture);
        tableLectureLatest.appendChild(div_root);
    }

    // 渲染正在进行的讲座
    for(var i=0; i<underwayLectureDto.length; i++){
        var e = underwayLectureDto[i];

        var panelDiv = document.createElement("div");

        panelDiv.setAttribute("class", "weui-panel");
        var posterDiv = document.createElement("div");
        posterDiv.setAttribute("class", "poster");
        var posterTextDiv = document.createElement("div");
        posterTextDiv.setAttribute("class", "poster_text");
        var imageA = document.createElement("a");
        imageA.setAttribute("href", e.wxNewsUrl);
        var img = document.createElement("img");
        img.setAttribute("class", "poster_image");
        img.setAttribute("src", "/sms/resources/wechat/upload/images/" + e.snapshoot);
        var textP = document.createElement("p");
        textP.innerHTML = e.title;

        var textA = document.createElement("a");
        textA.setAttribute("href", "javascript:void(0);");
        if(e.signIn==1){
            textA.innerHTML = "已签到";
            textA.setAttribute("class", "weui-btn weui-btn_mini weui-btn_primary weui-btn_disabled");
        }else{
            textA.innerHTML = "签到";
            textA.setAttribute("class", "weui-btn weui-btn_mini weui-btn_primary");
            textA.setAttribute("onclick", "signIn(" + e.id + " )");
        }

        posterTextDiv.appendChild(textP);
        posterTextDiv.appendChild(textA);
        imageA.appendChild(img);
        posterDiv.appendChild(imageA);
        panelDiv.appendChild(posterDiv);
        panelDiv.appendChild(posterTextDiv);
        tableLectureUnattend.appendChild(panelDiv);
    }

    // 渲染已经过去的讲座，签到和未签到的都有显示，只有文本和链接，没有海报
    for(var i=0; i<attendedLecture.length; i++){
        var e = attendedLecture[i];

        var panelDiv = document.createElement("div");

        panelDiv.setAttribute("class", "weui-panel");

        var posterTextDiv = document.createElement("div");
        posterTextDiv.setAttribute("class", "poster_text");

        var textA = document.createElement("a");
        textA.innerHTML = e.title;
        textA.setAttribute("href", e.wxNewsUrl);

        var textP = document.createElement("p");
        var date = new Date(e.startTime);
        textP.innerHTML = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();

        posterTextDiv.appendChild(textA);
        posterTextDiv.appendChild(textP);

        panelDiv.appendChild(posterTextDiv);
        tableLectureAttended.appendChild(panelDiv);
    }
}

function takeLecture(lectureId){
    $.alert("确定要参加该活动？", "提示", function() {
        $.ajax({
            type: "get",
            url: "/sms/admin/studentLecture/enroll/" + lectureId,
            dataType: "json",
            success: function (wechatReturn) {
                var attendBtn = $("#lecture" + lectureId);
                attendBtn.addClass("weui-btn_disabled");
                attendBtn.removeAttr("onclick");
                attendBtn.html("已报名");
                $.alert("参加活动成功！");
            },
            error: function (wechatReturn) {
                $.alert("参加活动失败！");
            }
        });
    });
}

function signIn(lectureId){
    window.location.href="lectureSignIn/" + lectureId;
}